package cn.njyd.corejava.thread.myday05;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

class ClickZhan{
    private long num = 0;

    synchronized public void synClick(){
        num++;
    }

    private AtomicLong atomicLong = new AtomicLong(0);

    public void atomicLongClick(){
        atomicLong.incrementAndGet();

    }
    private LongAdder longAdder = new LongAdder();

    public void longAdderClick(){
        longAdder.increment();
    }
    private LongAccumulator longAccumulator = new LongAccumulator((m,n)->{
        return m+n;
    },0);

    public void longAccumulatorClick(){
        longAccumulator.accumulate(1);
    }

}

public class AtomicDemo01 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch c1 = new CountDownLatch(4);
        CountDownLatch c2 = new CountDownLatch(4);
        CountDownLatch c3 = new CountDownLatch(4);
        CountDownLatch c4 = new CountDownLatch(4);
        ClickZhan clickZhan = new ClickZhan();
        long start = System.currentTimeMillis();
        for(int i=1;i<=4;i++) {
            new Thread(()->{
                try{
                    for (int j=1;j<=1000000;j++) {
                        clickZhan.synClick();
                    }
                }finally {
                    c1.countDown();
                }
            }).start();
        }
        c1.await();
        long end = System.currentTimeMillis();
        System.out.println("采用synchronized方式进行点赞共用时:"+(end-start));

        start = System.currentTimeMillis();
        for(int i=1;i<=4;i++) {
            new Thread(()->{
                try{
                    for (int j=1;j<=1000000;j++) {
                        clickZhan.atomicLongClick();
                    }
                }finally {
                    c2.countDown();
                }
            }).start();
        }
        c2.await();
        end = System.currentTimeMillis();
        System.out.println("采用atomicLong方式进行点赞共用时:"+(end-start));

        start = System.currentTimeMillis();
        for(int i=1;i<=4;i++) {
            new Thread(()->{
                try{
                    for (int j=1;j<=1000000;j++) {
                        clickZhan.longAdderClick();
                    }
                }finally {
                    c3.countDown();
                }
            }).start();
        }
        c3.await();
        end = System.currentTimeMillis();
        System.out.println("采用longAdder方式进行点赞共用时:"+(end-start));

        start = System.currentTimeMillis();
        for(int i=1;i<=4;i++) {
            new Thread(()->{
                try{
                    for (int j=1;j<=1000000;j++) {
                        clickZhan.longAccumulatorClick();
                    }
                }finally {
                    c4.countDown();
                }
            }).start();
        }
        c4.await();
        end = System.currentTimeMillis();
        System.out.println("采用longAccumulator方式进行点赞共用时:"+(end-start));
    }

}
